package com.adingxiong.cft.service.impl;

import com.adingxiong.cft.entity.Role;
import com.adingxiong.cft.errors.Assert;
import com.adingxiong.cft.mapper.RoleMapper;
import com.adingxiong.cft.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author xiongc
 * @since 2020-09-25
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Override
    public boolean add(Role role) {
        Role r = getRoleByName(role.getName());
        if(r != null){
            Assert.fail("该角色已经存在");
        }
        role.setCreateTime(new Date());
        return save(role);

    }

    @Override
    public Role getRoleByName(String name) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Role::getName ,name);
        Role role = getOne(wrapper);
        return  role;
    }

    @Override
    public List<Role> list(String name) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            wrapper.lambda().like(Role::getName,name);
        }
        List<Role> res = list(wrapper);
        return res;
    }

    @Override
    public List<Role> getUserRole(Integer id) {
        return baseMapper.listByUserId(id);
    }
}
